package com.hgy.commonsale.service.impl;

import java.util.List;

import org.geln.web.hibernate.BaseHibernateService;
import org.geln.web.status.Status;
import com.hgy.commonsale.product.Goods;
import com.hgy.commonsale.service.GoodManagerService;

import util.CommonUtil;

public class GoodManagerServiceImpl extends BaseHibernateService implements
		GoodManagerService {

	public Status deleteObjectByName(String name) {
		
		Goods goodDel=getGoodByGoodName(name);
		if(CommonUtil.isNotEmpty(goodDel)){
			getHibernateTemplate().delete(goodDel);
			return Status.SUCCESS;
		}else{
			return Status.FAILED_FOR_NULL;
		}
		
	}

	public Goods getGoodByGoodName(String goodName) {
		// TODO Auto-generated method stub
		String hqlQuery="from Goods goods where goods.goodName=?";
		
		List<Goods> lstGoods=getHibernateTemplate().find(hqlQuery, new String[]{goodName});
		if(lstGoods!=null&&lstGoods.size()>0){
			return lstGoods.get(0);
		}
		else{
			return null;
		}
		
	}

	public List<Goods> getGoodsList() {
		String hqlQeury="from Goods";
		
		List<Goods> lstGoods=getHibernateTemplate().find(hqlQeury);
		if(lstGoods!=null&&lstGoods.size()>0){
			return lstGoods;
		}else{
			return null;
		}
	}

	public Object saveObject(Goods good) {
		// TODO Auto-generated method stub
		if(CommonUtil.isEmpty(getGoodByGoodName(good.getGoodName()))){
			
			 getHibernateTemplate().saveOrUpdate(good);
			 return Status.SUCCESS;
			 
		}else{
			return Status.FAILED;
		}

	}

}
